/**
 * JavaScript wrapper for POST an GET requsets.
 * Require dojo-relase-1.0.2
 *
 * Copyright (C)
 * This program is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation. Either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. Without even the implied warranty
 * of ERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. Ff not, write to the Free Software
 * Fundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 * @author Bastian Schröder <bs@chemanet.de>
 * @version 2008-04-10
 * @package ajaxBridge
 */

// this url is used by GET and POST functions
var ajaxBridge_url = 'call.php?func=';


/**
 * Make a ajax call.
 *
 * @param string func the name of the PHP function called from javascript ajax
 * @param function response_func load( ajax_response )
 * @return string
 */
function ajax_call ( func, response_func ){
	dojo.xhrGet( {
		url: ajaxBridge_url + func,
		handleAs: 'text',
		timeout: 5000, // Time in milliseconds

		parent_load: response_func,

		// The LOAD function will be called on a successful response.
		load: function(response, ioArgs) {
			var ajax_response = eval('(' + response + ')');

			// if an error occurrence
			if( ajax_response.type == 'ajax_error_response' )
				console.error("ajax_error_response: ", ajax_response.exceptions[0]);

			this.parent_load.load( ajax_response );
			return response;
		},

		// The ERROR function will be called in an error case.
		error: function(response, ioArgs) {
			console.error("HTTP status code: ", ioArgs.xhr.status);
			return response;
		}
	});
}


/**
 * Make a ajax call over POST.
 *
 * @param string formularID
 * @param string func the name of the PHP function called from javascript ajax
 * @param function response_func load( ajax_response )
 * @return string
 */
function ajax_call_post( form, func, response_func ){
	dojo.xhrPost( {
		form: dojo.byId( form ),
		url: ajaxBridge_url + func,
		handleAs: 'text',
		timeout: 5000, // Time in milliseconds

		parent_load: response_func,

		// The LOAD function will be called on a successful response.
		load: function(response, ioArgs) {
			var ajax_response = eval('(' + response + ')');

			// if an error occurrence
			if( ajax_response.type == 'ajax_error_response' )
				console.error("ajax_error_response: ", ajax_response.exceptions[0]);

			this.parent_load.load( ajax_response );
			return response;
		},

		// The ERROR function will be called in an error case.
		error: function(response, ioArgs) {
			console.error("HTTP status code: ", ioArgs.xhr.status);
			return response;
		}
	});
}